const app = getApp();
Page({


  data: {
    id: '',
    password: '',
    nickName: '',
    portrait:''
  },

  getID(e){
    console.log("账号", e.detail.value)
    this.setData({
      id: e.detail.value
    })
  },

  getPassword(e){
    console.log("密码", e.detail.value)
    this.setData({
      password: e.detail.value
    })
  },
  navigateToIndex(){
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  login(){
     let id = this.data.id;
     let password = this.data.password;
     console.log('账号', id, '密码', password)
     if(id.length != 8)
     {
       wx.showToast({
         icon: 'none',
         title: '账号长度必须为8个字符'
       })
       return
     }
     
     wx.cloud.database().collection('user').where({
       id: id
     }).get({
       success(res){
         console.log("获取数据成功", res)
         let user=res.data[0]
         
         if(user.pasword == password)
         {
            console.log("登录成功")
            app.globalData.userID = user.id
            app.globalData.nickname = user.name
            console.log("全局变量", app.globalData.userID)
            console.log("全局变量", app.globalData.nickname)
            wx.showToast({
              title: '登录成功',
            })
           wx.switchTab({
              url: '../individual/individual',
              success: function(e){
                var page = getCurrentPages().pop();
                page.data.nickName = app.globalData.nickname;
                page.data.userID = app.globalData.userID;
              }
            })
            //保存登录状态
            wx.setStorageSync('user', user)
         }
         else{
           console.log('登录失败')
           wx.showToast({
            title: '账号或密码不正确',
            icon: 'none'
          })
         }
       },
       fail(res){
         
       }
     })
  }
  
})



